package org.osami.commons.dialog.android.androiddisplayservice;

import com.prosyst.mbs.android.application.ActivityWrapper;

import android.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Handler.Callback;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.view.KeyEvent;

public class MyView extends View /*EditText*/ implements android.os.Handler.Callback {

	private ActivityWrapper context;
	private MyApplicationFactory factory;
	private Handler messageHandler;
	
	public static final int WHAT_QUESTION = 1;
	public static final int WHAT_MESSAGE = 2;
	
	public Handler getMessageHandler() {
		return messageHandler;
	}
	
	public MyView(ActivityWrapper context, MyApplicationFactory factory) {
		super(context);
		
		this.context = context;
		this.factory = factory;
		
		messageHandler = new Handler(this) {
			
			public void handleMessage(Message msg) {
				// we don't need this anymore since MyView implements android.os.Handler.Callback
			}
			
		};
		
//		Log.i(Activator.tag(), "Creating notification");
//		
//		String ns = Context.NOTIFICATION_SERVICE;
//		NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(ns);
//		
//		int icon = R.drawable.arrow_down_float;
//		CharSequence tickerText = "Hello";
//		long when = System.currentTimeMillis();
//
//		Notification notification = new Notification(icon, tickerText, when);
//		
//		Context appContext = context.getApplicationContext();
//		CharSequence contentTitle = "My notification";
//		CharSequence contentText = "Hello World!";
//		Intent notificationIntent = new Intent();
//		//notificationIntent.setComponent(new ComponentName("com.prosyst.mbs.mobile.android", "com.prosyst.mbs.android.applications.OSGiPrograms"));
//		notificationIntent.setAction("com.prosyst.mbs.APPLICATION");
//		notificationIntent.putExtra("mbs.android.application.type", "org.osami.commons.dialog.android.view");
//		PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
//
//		notification.setLatestEventInfo(appContext, contentTitle, contentText, contentIntent);
//		
//
//		mNotificationManager.notify(1, notification);
//		
//		Log.i(Activator.tag(), "Notification created");
		
	}
	
	public boolean handleMessage(Message msg) {
		
//		Log.i(Activator.tag(), "Creating notification");
//		
//		String ns = Context.NOTIFICATION_SERVICE;
//		NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(ns);
//		
//		int icon = R.drawable.arrow_down_float;
//		CharSequence tickerText = "Hello";
//		long when = System.currentTimeMillis();
//
//		Notification notification = new Notification(icon, tickerText, when);
//		
//		Context appContext = context.getApplicationContext();
//		CharSequence contentTitle = "My notification";
//		CharSequence contentText = "Hello World!";
//		Intent notificationIntent = new Intent();
//		//notificationIntent.setComponent(new ComponentName("com.prosyst.mbs.mobile.android", "com.prosyst.mbs.android.applications.OSGiPrograms"));
//		notificationIntent.setAction("com.prosyst.mbs.APPLICATION");
//		notificationIntent.putExtra("mbs.android.application.type", "org.osami.commons.dialog.android.view");
//		PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
//
//		notification.setLatestEventInfo(appContext, contentTitle, contentText, contentIntent);
//		
//
//		mNotificationManager.notify(1, notification);
//		
//		Log.i(Activator.tag(), "Notification created");
		
		switch (msg.what) {
			case WHAT_QUESTION:
				Log.i(Activator.tag(), "Handling question");
				Question question = (Question)msg.obj;
				if (null != question) {
					Log.i(Activator.tag(), "Question = " + question.getMessage() + ", num options = " + question.getOptions().length);
					factory.showQuestion(question.getMessage(), question.getOptions(), context);
				} else {
					Log.i(Activator.tag(), "Malformed question data");
				}
				break;
			case WHAT_MESSAGE:
				Log.i(Activator.tag(), "Handling message");
				factory.showMessage((String)msg.obj, context);
				break;
			case 123:
				Log.i(Activator.tag(), "Starting external activity");
				Intent intent = new Intent();
				intent.setComponent(new ComponentName("com.prosyst.mbs.mobile.android", "com.prosyst.mbs.android.applications.OSGiPrograms"));
				context.startActivity(intent);
				break;
			default:
				Log.i(Activator.tag(), "Can not handle message due to unknown what field");
				return false;
		}
		
		return true;
	}

}
